package com.online.findagentserver.service;

import com.online.findagentserver.domain.Session;
import com.online.findagentserver.domain.SessionObj;
import com.online.findagentserver.domain.Ssc;
import com.online.findagentserver.slave0.Message0Mapper;
import com.online.findagentserver.slave0.Session0Mapper;
import com.online.findagentserver.slave0.Ssc0Mapper;
import com.online.findagentserver.slave1.Message1Mapper;
import com.online.findagentserver.slave1.Session1Mapper;
import com.online.findagentserver.slave1.Ssc1Mapper;
import com.online.findagentserver.utils.SpringApplicationContextHolder;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @Author: liuqing
 * @Despriction:
 * @Date:Created in 2018/4/24 17:02
 * @Modify By:
 */
@Service
public class FindAgentService {

    @Autowired
    private Ssc0Mapper sscMapper0;

    @Autowired
    private Ssc1Mapper sscMapper1;

    @Autowired
    private Session1Mapper sessionMapper1;

    @Autowired
    private Session0Mapper sessionMapper0;

    public SqlSessionFactory getSqlSessionFactory0() {
        return (SqlSessionFactory) SpringApplicationContextHolder.getApplicationContext().getBean("slave0SqlSessionFactory");
    }

    public SqlSessionFactory getSqlSessionFactory1() {
        return (SqlSessionFactory) SpringApplicationContextHolder.getApplicationContext().getBean("slave1SqlSessionFactory");
    }

    private SqlSession getSession(SqlSessionFactory sqlSessionFactory) {
        return sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    }

    public int saveSsc(int serviceId,int sessionId,int companyId){
        Ssc ssc = new Ssc(serviceId,null,sessionId,companyId,new Timestamp(System.currentTimeMillis()));
        int mod = companyId % 2;
        if (mod == 0) {
            this.sscMapper0.insertSSc(ssc);
        }else {
            this.sscMapper1.insertSSc(ssc);
        }
        return ssc.getId();
    }

    public List<Integer> getServiceId(int companyId, int sessionId) {
        int mod = companyId % 2;
        if (mod == 0) {
            return this.sscMapper0.getSsc(sessionId);
        } else {
            return this.sscMapper1.getSsc(sessionId);
        }
    }

    public void endTime(List<SessionObj> sessionObjs, int companyId) {
        SqlSession session0 = getSession(getSqlSessionFactory0());
        SqlSession session1 = getSession(getSqlSessionFactory1());

        Message0Mapper message0Mapper = session0.getMapper(Message0Mapper.class);
        Message1Mapper message1Mapper = session1.getMapper(Message1Mapper.class);

        try {
           int mod = companyId % 2;

           SessionObj sessionObj;
           if (mod == 0) {
               for (int i = 0; i < sessionObjs.size(); i++) {
                   sessionObj = sessionObjs.get(i);
                   message0Mapper.updateMsgEndTime(new Timestamp(System.currentTimeMillis()), sessionObj.getSessionId());
               }
           } else {
               for (int i = 0; i < sessionObjs.size(); i++) {
                   sessionObj = sessionObjs.get(i);
                   message1Mapper.updateMsgEndTime(new Timestamp(System.currentTimeMillis()), sessionObj.getSessionId());
               }
           }
           session0.commit();
           session0.clearCache();
           session1.commit();
           session1.clearCache();
        } catch (Exception e) {
           session0.rollback();
           session1.rollback();
           System.out.println(e.getMessage());
        } finally {
           session0.close();
           session1.close();
        }
    }

    public int getSessionId(int companyId, String openId) {
        Session session = new Session(new Timestamp(System.currentTimeMillis()),null,companyId,openId,"");
        int mod = companyId % 2;
        if (mod == 0) {
            sessionMapper0.insertSSc(session);
        } else {
            sessionMapper1.insertSSc(session);
        }
        return session.getId();
    }

    public int getAId(int companyId, String openId) {

        int mod = companyId % 2;
        if (mod == 0) {
            Integer sessionId = this.sscMapper0.getSessionId(openId);
            if (sessionId != null) {
                Integer aId = this.sscMapper0.getAId(sessionId);
                if (aId != null) {
                    return aId;
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        } else {
            Integer sessionId = this.sscMapper1.getSessionId(openId);
            if (sessionId != null) {
                Integer aId = this.sscMapper1.getAId(sessionId);
                if (aId != null) {
                    return aId;
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        }
    }
}